let global_page = 1,
  global_limit = 5,
  goodsUrl = '/goods';

function loadGoods(name) {
  initLayuiInfo();
  name = name === 'undefined' ? '' : name;
  $.get(goodsUrl + '/findAllByPage', {page: global_page, limit: global_limit, name: name}, types => {
    if (types.data.length === 0 && global_page > 1) {
      global_page--;
      loadGoods();
    } else {
      loadGoodsList(types);
      loadGoodsLayPage(types);
      loadEchartsInfo();
    }
  });
}

function loadGoodsList(types) {
  layui.use(['table', 'layer'], () => {
    let table = layui.table,
      layer = layui.layer;
    table.render({
      elem: '#type',
      data: types.data,
      limit: global_limit,
      toolbar: '#type-toolbar',
      cols: [
        [
          {field: 'id', title: '商品种类ID'},
          {field: 'name', title: '商品种类名称'},
          {field: 'ctime', title: '商品种类创建时间'},
          {field: 'utime', title: '商品种类信息更新时间'},
          {title: '操作', toolbar: '#type-tool'}
        ]
      ]
    });
    table.on('toolbar(type)', data => {
      let event = data.event;
      switch (event) {
        case 'add':
          showLayout($('#add-type-layout'));
          break;
        case 'search':
          let goodName = $('#search').val();
          loadGoods(goodName);
          break;
      }
    });
    table.on('tool(type)', elem => {
      let event = elem.event,
        data = elem.data;
      layer.confirm('确定要删除此种类吗?', {
        btn: ['确定', '取消']
      }, () => {
        $.get(goodsUrl + '/findGoodsByPage', {page: 1, limit: 1, type: data.name}, goods => {
          if (goods.data.length !== 0) {
            layer.msg('该商品种类下还有商品，无法删除!');
          } else {
            $.post(goodsUrl + '/delGoodsType/' + data.id, result => {
              result = result > 0 ? '成功' : '失败';
              layer.msg('商品种类删除' + result + '!');
              loadGoods();
            });
          }
        });
      });
    });
  });
}

function loadGoodsLayPage(types) {
  layui.use('laypage', () => {
    layui.laypage.render({
      elem: 'type-page',
      curr: global_page,
      limit: global_limit,
      limits: [5, 10, 20, 50],
      count: types.count,
      layout: ['count', 'prev', 'page', 'next', 'limit'],
      jump: function (obj, first) {
        if (!first) {
          global_page = obj.curr;
          global_limit = obj.limit;
          loadGoods();
        }
      }
    });
  });
}

function initLayuiInfo() {
  layui.use(['element', 'form', 'layer'], () => {
    let form = layui.form,
      layer = layui.layer;
    form.on('submit(add)', data => {
      let field = data.field;
      $.post(goodsUrl + '/addGoodsType', field, result => {
        result = result > 0 ? '成功' : '失败';
        layer.msg('商品类别添加' + result + '!');
        hideLayout($('#add-type-layout'));
        loadGoods();
      });
    });
  });
}

function loadEchartsInfo() {
  $.get(goodsUrl + '/getEcharts', data => {
    setTimeout(() => {
      echarts.init(document.getElementById('echarts')).setOption({
        title: {
          text: '商品种类下商品数量统计',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          type: 'scroll',
          orient: 'vertical',
          right: 10,
          top: 20,
          bottom: 20,
          data: data.legendData
        },
        series: [
          {
            name: '种类',
            type: 'pie',
            radius: '55%',
            center: ['40%', '50%'],
            data: data.seriesData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      });
    }, 500);
  });
}

loadEchartsInfo();